import React from 'react'
import PropTypes from 'prop-types'
import Component, { mapStateToProps, mapDispatchToProps } from '@components/Component'
import { connect } from 'react-redux'
import { message } from 'antd'
import Approve from '@components/ApprovePre/Index'
import StepOneInput from './components/StepOneInput'
import StepOneHistory from './components/StepOneHistory'
import { requestFiles } from '@js/request'
import { awaitWrap } from '@js/tool'

class Avoid extends Component {
  // 提交前处理
  beforeSubmit (instance, data) {
    switch (instance.currentStep.step_key) {
      // case 'tuichuhuibi01':
      //   return this.stepOneInputBeforeSubmit(instance, data)
      default:
        return data
    }
  }

  // 第一步提交
  async stepOneInputBeforeSubmit (instance, data) {
    const hide = message.loading('提交中', 120)
    let fileid = ''
    const file = data.file ? data.file.fileList : []
    if (file && file.length) {
      const [e, d] = await awaitWrap(requestFiles(file))
      if (e) {
        hide()
        return message.warning(e.message)
      }
      fileid = d.map(li => li.id).join()
    }
    hide()
    data.vars.challenge_fileid = fileid
    data.vars.challenge_submittalid = data.vars.challenge_submittalid.join()
    data.vars.challenge_challenged = JSON.stringify(data.vars.challenge_challenged)
    delete data.file
    return data
  }

  // 渲染当前操作步骤线上部分
  renderCurrentStepLineTop (formRef, instance, renderFn) {
    switch (instance.currentStep.step_key) {
      case 'closure_refund01':
        return <StepOneInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} />
      default:
        return renderFn()
    }
  }

  // 渲染历史
  renderHistory (action, renderFn, instance, renderDoc) {
    const key = action.step_key
    if (key === 'closure_refund01') {
      return <StepOneHistory key={action.id} userInfo={this.props.userInfo} instance={instance} action={action} caseDetail={this.props.caseDetail} renderDoc={renderDoc} />
    }
    return renderFn(action)
  }

  render () {
    return (
      <div>
        <Approve
          caseDetail={this.props.caseDetail}
          flowKey='jieantuifei'
          afterAction={(instance, action) => this.props.refreshCaseData()}
          creatable={this.props.editable}
          renderCurrentStepLineTop={(...params) => this.renderCurrentStepLineTop(...params)}
          renderHistory={(h, renderFn, instance, renderDoc) => this.renderHistory(h, renderFn, instance, renderDoc)}
          // initialCurrentStepFormData={instance => this.getFormInitialData(instance)}
          setBizid={() => Promise.resolve(this.props.caseDetail.claim.id)}
          beforeSubmit={(...params) => this.beforeSubmit(...params)}
          multiply
        />
      </div>
    )
  }
}

Avoid.propTypes = {
  // caseDetail: PropTypes.object,
  // active: PropTypes.bool,
  refreshCaseData: PropTypes.func,
  editable: PropTypes.bool
  // fileList: PropTypes.array
  // getFileList: PropTypes.func
}
Avoid.defaultProps = {
  caseDetail: {},
  active: false,
  refreshCaseData: () => {},
  editable: false,
  fileList: [],
  getFileList: () => {}
}

const mapState = mapStateToProps(function (state) {
  return {
    userInfo: state.basic.userInfo
  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {

  }
})

export default connect(mapState, mapDispatch)(Avoid)
